package my_collection;

import java.util.Arrays;

public class MyListTest {

	public static void main(String[] args) {
		MyList<Integer> list = new MyList<>();
		for(int i=0;i<5;i++) {
			list.add(i);
		}
		System.out.println(list.size());
		System.out.println(list.get(0));
		for(int i=0;i<4;i++) {
			list.add(i);
		}
		System.out.println(list.size());
		System.out.println(list);
	}
}

class MyList<T>{
	private Object[] list = null;
	private int size;
	private int capacity = 5;
	public MyList() {
		list = new Object[capacity];
		size = 0;
	}
	
	public boolean add(Object o) {
		ensureCapacity();
		list[size++] = o;
		return true;
	}
	
	public T get(int i) {
		if(i<size) {
			return (T)list[i];
		}
		return null;
	}
	
	public int size() {
		return size;
	}
	
	private void ensureCapacity() {
		if(size==capacity) {
			capacity = size/2+size;
			list = Arrays.copyOf(list, capacity);
		}
	}

	@Override
	public String toString() {
		return "MyList [list=" + Arrays.toString(list) + ", size=" + size + ", capacity=" + capacity + "]";
	}

}


